<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>26ref和refs的使用</title>
</head>
<body>
<div id="app">
    <!--a、注册组件的ref-->
    <input type="text" value="Mr.GuoFu" ref="userFu" />
    <outer-assembly ref="child"></outer-assembly>
</div>
<script src="../vue.global.3.0.5.js"></script>
<script>
    const outerAssembly = {
        data() {
            return {
                
            }
        },
        template: `
        <input type="text" value="Mr.GuoZi" ref="userZi" />
        `
    }

    //1、创建一个Vue应用实例，本身是最顶层的根组件
    const app = Vue.createApp({
        data() {
            return { 
                show: true
            }
        },
        components: {
            //命名规则必须是小写字母加-
            "outer-assembly": outerAssembly
        },
        created() {
            //初始化时无法获取ref
            console.log(this.$refs.userFu);
        },
        mounted() {
            //c、父组件调用自己的ref
            console.log("父组件mounted()：" + this.$refs.userFu.value);
            //d、父组件调用子组件的ref
            console.log("父组件mounted()：" + this.$refs.child.$refs.userZi.value);
        },
    });
    //2、定义一个全局组件
    app.component('button-counter', {
        //数据
        data() {
            return {
                counter: 100
            }
        },
        //模板
        template: `
            计算器: {{counter}}
            <button v-on:click="counter++">counter++</button><br>
        `
    });
    //3、挂载，返回值是代理对象并不是应用实例本身
    const vm = app.mount("#app");
    //b、获取组件本身的ref值
    console.log(vm.$refs.userFu.value);
</script>
</body>
</html>